package util;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.util.Date;

/**
 * @program: personnel_management_system
 * @description: 连接池连接mysql
 * @author: Mr.Sun
 * @create: 2020-05-24 20:58
 **/

public class MysqlConnUtil {
    private static DruidDataSource dataSource = null;
    private static void initDataSource() throws Exception{
        if(dataSource == null){
            dataSource = new DruidDataSource();
            dataSource.setUrl("jdbc:mysql://localhost:3306/personsystem?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8");   //数据库地址
            dataSource.setUsername("root");   //数据库账号
            dataSource.setPassword("konodioda2019");    //数据库密码
            dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");   //加载mysql驱动包

            dataSource.setInitialSize(2);   //初始连接数
            dataSource.setMinIdle(2);       //最小连接数
            dataSource.setMaxActive(10);   //最大连接数

            dataSource.setMaxWait(20000);   //最大响应时间
            dataSource.setTimeBetweenEvictionRunsMillis(20000);   //链接最大空闲时间
            dataSource.setValidationQuery("select 'x'");     //向数据库请求数据防止被断开链接，类似ping
            dataSource.setTestWhileIdle(true);
            dataSource.setTestOnBorrow(true);

        }
    }

    public static DataSource getDataSource() throws Exception {
        initDataSource();
        return dataSource;
    }
}
